package com.component;

import com.Listener.ActionDoneListener;
import com.bean.Users;
import com.dao.UserDao;
import com.util.ScreenUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddConsumerDialog extends JDialog {
    final int WIDTH = 500;
    final int HEIGHT = 400;

    private ActionDoneListener listener;

    public AddConsumerDialog(JFrame jf, String title, boolean isModel,ActionDoneListener listener){
        super(jf,title,isModel);
        this.listener  = listener;
        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装用户编号
        Box IDBox = Box.createHorizontalBox();
        JLabel IDLabel = new JLabel("用户编号：");
        JTextField IDField = new JTextField(15);

        IDBox.add(IDLabel);
        IDBox.add(Box.createHorizontalStrut(15));
        IDBox.add(IDField);

        //组装用户类型
        Box typeBox = Box.createHorizontalBox();
        JLabel typeLabel = new JLabel("用户类型：");
        String[] userTypes = {"消费者", "农户", "管理员"};
        JComboBox<String> typeComboBox = new JComboBox<>(userTypes);
        typeComboBox.setAlignmentX(Component.CENTER_ALIGNMENT);

        typeBox.add(typeLabel);
        typeBox.add(Box.createHorizontalStrut(15));
        typeBox.add(typeComboBox);

        //组装用户名
        Box nameBox = Box.createHorizontalBox();
        JLabel nameLabel = new JLabel("用  户  名：");
        JTextField nameField = new JTextField(15);

        nameBox.add(nameLabel);
        nameBox.add(Box.createHorizontalStrut(15));
        nameBox.add(nameField);

        //组装登录密码
        Box passwordBox = Box.createHorizontalBox();
        JLabel passwordLabel = new JLabel("登陆密码：");
        JTextField passwordField = new JTextField(15);

        passwordBox.add(passwordLabel);
        passwordBox.add(Box.createHorizontalStrut(15));
        passwordBox.add(passwordField);

        //组装性别
        Box genderBox = Box.createHorizontalBox();
        JLabel genderLabel = new JLabel("  性     别：");
        JComboBox<String> ConsumerGenderBox = new JComboBox<>();
        ConsumerGenderBox.addItem("男");
        ConsumerGenderBox.addItem("女");

        genderBox.add(genderLabel);
        genderBox.add(Box.createHorizontalStrut(20));
        genderBox.add(ConsumerGenderBox);

        //组装联系电话
        Box phoneBox = Box.createHorizontalBox();
        JLabel phoneLabel = new JLabel("联系电话：");
        JTextField phoneField = new JTextField(15);

        phoneBox.add(phoneLabel);
        phoneBox.add(Box.createHorizontalStrut(15));
        phoneBox.add(phoneField);

        //组装电子邮箱
        Box emailBox = Box.createHorizontalBox();
        JLabel emailLabel = new JLabel("电子邮箱：");
        JTextField emailField = new JTextField(15);

        emailBox.add(emailLabel);
        emailBox.add(Box.createHorizontalStrut(  15));
        emailBox.add(emailField);

        //组装家庭住址
        Box addressBox = Box.createHorizontalBox();
        JLabel addressLabel = new JLabel("家庭住址：");
        JTextField addressField = new JTextField(15);

        addressBox.add(addressLabel);
        addressBox.add(Box.createHorizontalStrut(15));
        addressBox.add(addressField);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton addBtn = new JButton("添加");
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户的录入
                int UserID = Integer.valueOf(IDField.getText().trim());
                String UserType = (String) typeComboBox.getSelectedItem();
                String Username = nameField.getText().trim();
                String Password = passwordField.getText().trim();
                String Gender = (String) ConsumerGenderBox.getSelectedItem();
                String PhoneNumber = phoneField.getText().trim();
                String Email = emailField.getText().trim();
                String Address = addressField.getText().trim();

                //将数据添加进数据库
                if(Username.length()==0||Password.length()==0)
                {
                    JOptionPane.showMessageDialog(jf, "添加失败，请添加用户名或密码！");
                }
                else if(Gender.length()==0||Email.length()==0||Address.length()==0){
                    JOptionPane.showMessageDialog(jf, "添加失败，请继续完善信息！");
                }
                else if(!Username.matches("\\w+"))
                {
                    JOptionPane.showMessageDialog(jf, "添加失败，用户名只能包含英文数字下划线!");
                }
                else if(Password.length()<=8)
                {
                    JOptionPane.showMessageDialog(jf, "添加失败，用户密码需要8位以上!");
                }
                else if(!PhoneNumber.matches("\\d{11,12}")){
                    JOptionPane.showMessageDialog(jf, "联系电话是11或12位数字组成!");
                }
                else{
                    Users user = new Users(UserID,UserType,Username,Password,Gender,PhoneNumber,Email,Address);
                    new UserDao().insertUser(user);
                    JOptionPane.showMessageDialog(jf, "添加成功!");
                    listener.done(null);
                    dispose(); // 关闭添加窗口
                }
            }
        });


        btnBox.add(addBtn);

        vBox.add(Box.createVerticalStrut(20));
        vBox.add(IDBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(typeBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(nameBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(passwordBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(genderBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(phoneBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(emailBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(addressBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(btnBox);

        //为了左右有间距，在vBox外层封装一个水平的Box，添加间隔
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(50));
        hBox.add(vBox);
        hBox.add(Box.createHorizontalStrut(50));

        this.add(hBox);
    }
}
